﻿/*************************************************************************************
 * 文 件 名:   HK_SimpleVideoCtrl
 * 
 * 描    述:   简易视频播放功能
 * 
 * 版    本：  V1.0
 * 创 建 者：  谭潇杰、京产肠饭
 * 创建时间：  2024/2/7
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using QFramework;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace HKTools
{
    public class HK_SimpleVideoCtrl : MonoBehaviour
    {
        [Header("VideoPlayer组件"), SerializeField] VideoPlayer videoPlayer;

        // 视频暂停提示
        Image pause_Img;
        bool isPause = false;

        void Awake()
        {
            pause_Img = transform.Find("Pause_Img").GetComponent<Image>();
        }

        void OnEnable()
        {
            pause_Img.Hide();
            videoPlayer.targetTexture.Release(); ;
        }

        /// <summary>
        /// 通过 VideoClip 播放视频
        /// </summary>
        /// <param name="videoClip"></param>
        public void PlayClipVideo(VideoClip videoClip, bool isLooping = false)
        {
            if (videoPlayer.source != VideoSource.VideoClip)
                videoPlayer.source = VideoSource.VideoClip;

            videoPlayer.isLooping = isLooping;

            pause_Img.Hide();

            videoPlayer.SetDirectAudioMute(0, false);

            if (videoClip != null)
                videoPlayer.clip = videoClip;

            videoPlayer.Play();

            isPause = false;
        }

        /// <summary>
        /// 通过 URL 播放视频
        /// </summary>
        /// <param name="url"></param>
        public void PlayURLVideo(string url, bool isLooping = false)
        {
            if (videoPlayer.source != VideoSource.Url)
                videoPlayer.source = VideoSource.Url;

            videoPlayer.isLooping = isLooping;

            pause_Img.Hide();

            videoPlayer.SetDirectAudioMute(0, false);

            if (!string.IsNullOrEmpty(url))
                videoPlayer.url = url;

            videoPlayer.Prepare();

            videoPlayer.prepareCompleted += source =>
            {
                videoPlayer.Play();
            };

            isPause = false;
        }

        /// <summary>
        /// 通过 StreamingAssets 播放视频
        /// </summary>
        /// <param name="url"></param>
        public void PlayStreamingAssetsVideo(string videoName, bool isLooping = false)
        {
            if (videoPlayer.source != VideoSource.Url)
                videoPlayer.source = VideoSource.Url;

            videoPlayer.isLooping = isLooping;

            pause_Img.Hide();

            videoPlayer.SetDirectAudioMute(0, false);

            string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, videoName);

            videoPlayer.url = filePath;

            videoPlayer.Prepare();

            videoPlayer.prepareCompleted += source =>
            {
                videoPlayer.Play();
            };

            isPause = false;
        }

        /// <summary>
        /// 暂停视频
        /// </summary>
        public void PauseVideo()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            isPause = !isPause;
            pause_Img.gameObject.SetActive(isPause);

            if (!isPause)
                videoPlayer.Play();
            else
                videoPlayer.Pause();
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void StopVideo()
        {
            videoPlayer.Stop();
            videoPlayer.targetTexture.Release();

            isPause = false;
            pause_Img.Hide();

            videoPlayer.clip = null;
            videoPlayer.url = "";
        }
    }
}